define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "商品名称-搜索";};
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index' + location.search,
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    import_url: 'product/import',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'name_en', title: __('Name_en'), operate: 'LIKE'},
                        // {field: 'categoryproduct.name', title: __('Category_id')},
                        {field: 'categoryproduct.name', title: __('Category_id'), visible: true, addClass: "selectpage", extend: "data-source='category_product' data-field='name' data-primary-key='name'"},
                        {field: 'market_price', title: __('Market_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'descript', title: __('Descript'), operate: 'LIKE'},
                        {field: 'orderby', title: __('排序'),sortable: true,cellStyle: Controller.api.formatter.css2,operate: false},
                        {
                            field: 'status',
                            title: '商品状态',
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle,
                            sortable: true,
                            searchList: {0: __('无效'), 1: __('有效')},
                            cellStyle: Controller.api.formatter.css,
                        },

                        {
                            field: 'shelf_status',
                            title: '上架状态',
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle,
                            sortable: true,
                            searchList: {0: __('下架'), 1: __('上架')},
                            cellStyle: Controller.api.formatter.css,
                        },

                        {
                            field: 'top_picks',
                            title: '精选推荐',
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle,
                            sortable: true,
                            searchList: {0: __('否'), 1: __('是')},
                            cellStyle: Controller.api.formatter.css,
                        },

                        // {field: 'shelf_status', title: __('Shelf_status')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                // thumb: function (value, row, index) {
                //     return '<a href="' + row.BigPicAddress_obs_url + '" target="_blank"><img src="' + row.BigPicAddress_obs_url +'" alt="" style="max-height:90px;max-width:120px"></a>';
                //
                //     // console.log(row);return;
                //     // if (row.mimetype.indexOf("image") > -1) {
                //     //     return '<a href="' + row.fullurl + '" target="_blank"><img src="' + row.fullurl + row.thumb_style + '" alt="" style="max-height:90px;max-width:120px"></a>';
                //     // } else {
                //     //     return '<a href="' + row.fullurl + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>';
                //     // }
                // },
                css:function(){
                    return{
                        css:{
                            "min-width":"120px !important",
                            // "overflow":"hidden",
                            // "white-space":"nowrap",
                            // "text-overflow":"ellipsis",
                            // "display":"block",
                        }
                    };
                },
                css2:function(){
                    return{
                        css:{
                            "min-width":"80px !important",
                            // "overflow":"hidden",
                            // "white-space":"nowrap",
                            // "text-overflow":"ellipsis",
                            // "display":"block",
                        }
                    };
                },

            }
        }
    };
    return Controller;
});
